/*日志操作类
Created on 2020年05月10日
@author: zxyong 13738196011
 */

package com.zxy.adminlog;

import java.io.*;

import com.zxy.common.*;

public class UsAdmin_Log
{
	// 写入日志内容
	private StringBuilder	strContent		= null;
	// 自定义文件夹内容
	private String			strFolder		= "";
	// 定义文件名
	private String			strTFileName	= "";

	private File			fiFile			= null;

	public StringBuilder getStrContent()
	{
		return strContent;
	}

	public void setStrContent(StringBuilder Content)
	{
		this.strContent = Content;
	}

	public String getStrFolder()
	{
		return strFolder;
	}

	public void setStrFolder(String Folder)
	{
		this.strFolder = Folder;
	}

	public String getStrTFileName()
	{
		return strTFileName;
	}

	public void setStrTFileName(String TFileName)
	{
		this.strTFileName = TFileName;
	}

	public File getFiFile()
	{
		return fiFile;
	}

	public void setFiFile(File temfiFile)
	{
		this.fiFile = temfiFile;
	}

	public UsAdmin_Log(String strTemFolder,StringBuilder strValue,int Data)
	{
		this.setStrFolder(strTemFolder + Com_Para.zxyPath + "data");
		this.setStrContent(strValue);
	}

	// 写日志文件
	public UsAdmin_Log(String strTemFolder,StringBuilder strValue)
	{
		this.setStrFolder(strTemFolder + Com_Para.zxyPath + "log");
		this.setStrContent(strValue);
	}

	public UsAdmin_Log(String strTemFolder,StringBuilder strValue,String strFileName)
	{
		this.setStrFolder(strTemFolder + Com_Para.zxyPath + "log");
		this.setStrContent(strValue);
		this.setStrTFileName(strFileName);
	}

	public void WriteData(String delay_code) throws IOException
	{
		HadForder();
		SaveFileData(delay_code);
	}

	public void InitWriteData(String delay_code) throws IOException
	{
		HadForder();
		InitSaveFileData(delay_code);
	}

	// 写日志文件
	public void WriteLog() throws IOException
	{
		HadForder();
		SaveFileDay();
	}

	public void WriteLogAll(StringBuilder strSub) throws IOException
	{
		HadForder();
		SaveFileDayAll(strSub);
	}

	// 写日志文件
	public void WriteLogSub(String strSub) throws IOException
	{
		HadForder();
		SaveFileDay(strSub);
	}

	// 写日志文件
	public void WriteLogSub() throws IOException
	{
		HadForder();
		SaveFileDay("DEC_SEC_");
	}

	// 读数据文件
	public StringBuilder ReadData(String delay_code,String param_name) throws IOException
	{
		StringBuilder strResult = new StringBuilder();
		strTFileName = strFolder + Com_Para.zxyPath + delay_code + ".dat";
		fiFile = new File(strTFileName);
		FileInputStream fr = null;
		if(!fiFile.exists())
		{
			strResult.append("{\"" + param_name + "\":[{\"s_result\":\"0\",\"error_desc\":\"数据查询中或数据结果未生成,请稍候在试" + "\"}]}");
		}
		else
		{
			InputStreamReader is = null;
			BufferedReader br = null;
			try
			{
				fr = new FileInputStream(strTFileName);
				is = new InputStreamReader(fr,Com_Para.U_CODE);
				br = new BufferedReader(is);
				while(true)
				{
					String line = br.readLine();
					if(null == line)
						break;
					strResult.append(line);
				}
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
			finally
			{
				is.close();
				br.close();
				fr.close();
			}
		}
		return strResult;
	}

	// 接口数据以文件形式保存
	private void InitSaveFileData(String delay_code) throws IOException
	{
		strTFileName = strFolder + Com_Para.zxyPath + delay_code + ".dat";
		fiFile = new File(strTFileName);
		Writer sw = null;
		if(!fiFile.exists())
		{
			fiFile.createNewFile();
			sw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(strTFileName),Com_Para.U_CODE));
			sw.write(strContent.toString());
			sw.close();
		}
		else
		{
			sw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(strTFileName),Com_Para.U_CODE));
			sw.write(strContent.toString());
			sw.close();
		}
	}

	private void SaveFileData(String delay_code) throws IOException
	{
		strTFileName = strFolder + Com_Para.zxyPath + delay_code + ".dat";
		fiFile = new File(strTFileName);
		FileWriter sw = null;
		if(!fiFile.exists())
		{
			fiFile.createNewFile();
			sw = new FileWriter(strTFileName,true);
			sw.write(strContent.toString());
			sw.close();
		}
		else
		{
			sw = new FileWriter(strTFileName,true);
			sw.write(strContent.toString());
			sw.close();
		}
	}

	private void SaveFileDayAll(StringBuilder strTContent) throws IOException
	{
		String strLogName = "";
		if(this.getStrTFileName() == "")
			strLogName = strFolder + Com_Para.zxyPath + "zxyong_" + Com_Fun.DateTimeStr("yyyyMMdd") + "_0.log";
		else strLogName = strFolder + Com_Para.zxyPath + this.getStrTFileName() + ".log";
		fiFile = new File(strLogName);
		long lgSize = 0;
		FileWriter sw = null;
		if(!fiFile.exists())
		{
			fiFile.createNewFile();
			sw = new FileWriter(strLogName,true);
			sw.write("*********" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss") + "*********\r\n");
			sw.write(strContent.toString());
			sw.close();
		}
		else
		{
			lgSize = fiFile.length();
			int i = 1;
			while(true)
			{
				if(lgSize > Long.valueOf(1 * 1024 * 1024))
				{
					if(this.getStrTFileName() == "")
						strLogName = strFolder + Com_Para.zxyPath + "zxyong_" + Com_Fun.DateTimeStr("yyyyMMdd") + "_" + String.valueOf(i) + ".log";
					else strLogName = strFolder + Com_Para.zxyPath + this.getStrTFileName() + Com_Fun.DateTimeStr("yyyyMMdd") + "_" + String.valueOf(i) + ".log";
					fiFile = new File(strLogName);
					if(!fiFile.exists())
					{
						fiFile.createNewFile();
						sw = new FileWriter(strLogName,true);
						sw.write("*********" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss") + "*********\r\n");
						sw.write(strContent.toString());
						sw.close();
					}
					else
					{
						lgSize = fiFile.length();
					}
				}
				else
				{
					sw = new FileWriter(strLogName,true);
					sw.write("*********" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss") + "*********\r\n");
					sw.write(strContent.toString());
					sw.close();
					break;
				}
				i++;
			}
		}
	}

	// 按日保存日志文件
	private void SaveFileDay(String Sub) throws IOException
	{
		String strLogName = strFolder + Com_Para.zxyPath + "zxyong_" + Sub + Com_Fun.DateTimeStr("yyyyMMdd") + "_0.log";
		fiFile = new File(strLogName);
		long lgSize = 0;
		FileWriter sw = null;
		if(!fiFile.exists())
		{
			fiFile.createNewFile();
			sw = new FileWriter(strLogName,true);
			sw.write("*********" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss") + "*********\r\n");
			sw.write(strContent.toString());
			sw.close();
		}
		else
		{
			lgSize = fiFile.length();
			int i = 1;
			while(true)
			{
				if(lgSize > Long.valueOf(1 * 1024 * 1024))
				{
					strLogName = strFolder + Com_Para.zxyPath + "zxyong_" + Sub + Com_Fun.DateTimeStr("yyyyMMdd") + "_" + String.valueOf(i) + ".log";
					fiFile = new File(strLogName);
					if(!fiFile.exists())
					{
						fiFile.createNewFile();
						sw = new FileWriter(strLogName,true);
						sw.write("*********" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss") + "*********\r\n");
						sw.write(strContent.toString());
						sw.close();
						break;
					}
					else
					{
						lgSize = fiFile.length();
					}
				}
				else
				{
					sw = new FileWriter(strLogName,true);
					sw.write("*********" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss") + "*********\r\n");
					sw.write(strContent.toString());
					sw.close();
					break;
				}
				i++;
			}
		}
	}

	// 按日保存日志文件
	private void SaveFileDay() throws IOException
	{
		String strLogName = "";
		if(this.getStrTFileName() == "")
			strLogName = strFolder + Com_Para.zxyPath + "zxyong_" + Com_Fun.DateTimeStr("yyyyMMdd") + "_0.log";
		else strLogName = strFolder + Com_Para.zxyPath + this.getStrTFileName() + ".log";

		fiFile = new File(strLogName);
		long lgSize = 0;
		FileWriter sw = null;
		if(!fiFile.exists())
		{
			fiFile.createNewFile();
			sw = new FileWriter(strLogName,true);
			sw.write("*********" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss") + "*********\r\n");
			sw.write(strContent + "\r\n");
			sw.close();
		}
		else
		{
			lgSize = fiFile.length();
			int i = 1;
			while(true)
			{
				if(lgSize > Long.valueOf(1 * 1024 * 1024))
				{
					if(this.getStrTFileName() == "")
						strLogName = strFolder + Com_Para.zxyPath + "zxyong_" + Com_Fun.DateTimeStr("yyyyMMdd") + "_" + String.valueOf(i) + ".log";
					else strLogName = strFolder + Com_Para.zxyPath + this.getStrTFileName() + Com_Fun.DateTimeStr("yyyyMMdd") + "_" + String.valueOf(i) + ".log";
					fiFile = new File(strLogName);
					if(!fiFile.exists())
					{
						fiFile.createNewFile();
						sw = new FileWriter(strLogName,true);
						sw.write("*********" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss") + "*********\r\n");
						sw.write(strContent.toString());
						sw.close();
						break;
					}
					else
					{
						lgSize = fiFile.length();
					}
				}
				else
				{
					sw = new FileWriter(strLogName,true);
					sw.write("*********" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss") + "*********\r\n");
					sw.write(strContent.toString());
					sw.close();
					break;
				}
				i++;
			}
		}
	}

	// 判断文件夹是否存在，不存在则添加文件夹
	private void HadForder()
	{
		File file = new File(this.getStrFolder());
		if(!file.exists() && !file.isDirectory())
		{
			file.mkdir();
		}
	}
}